/*****************************************************************************
 * Twisted Twistor is Twisting 2/Camelot
 * Now slightly faster, and 1 byte smaller! :D
 * Code: Cruzer/CML
 * Asm'ed with: KickAss 2.25
 * Released: 2008-01-04
 *****************************************************************************/
.var barCnt =		$02
.var cnt =		$03
.var p0 =		$05
.var barOrder =		$07
.var cntStart =		$08
.var hia =		$0b
.var lo =		$0c
.var buffer =		$10

.var code =		$0801
.var screen =		$0c00
.var sine128 =		$1200
.var bitmap =		$2000

.pc = code "code"
//------------------------------------------------------------------------------
.var screenWidth = 40
.var screenHeight = 25
.var offsetX = 4

.var sineStarts = List().add(0,63)
.var sineSpread = 4
.var sineSpeedList = List().add(-4,3)
//------------------------------------------------------------------------------
basic:			.by $0b,$08,$39,$05,$9e,$32,$30,$37,$39,$00,$00,$00
//------------------------------------------------------------------------------
colors0:		.by $6b,$bb,$b5,$55,$53,$5d,$dd,$d1,$11
colors1:		.by $64,$24,$48,$4a,$ca,$af,$7f,$71,$11
//------------------------------------------------------------------------------
start:
			sei
//fill bitmap...
!:			lda #%01010101
sbm:			sta bitmap-$100,x
			lda #$00
			.by STA_ABSX,$02,$00 //sta $0002,x
			inx
			bne !-
			inc sbm+2
			bpl !-

			pha			

//calc "sine"...
			ldy #$ff
!loop:	
loa:			lda #$15
			adc #$07
			sta loa+1
			bcc !+
			inc hia
			clc
!:
			adc lo
			sta lo
			pla
			adc hia
			pha
			sta sine128,x
			sta sine128,y
			eor #$7f
			sta sine128+$80,x
			sta sine128-$80,y

			inx
			dey
			cpx #$40
			bne !loop-

			lda #$3b
			sta $d011
			sta $d018

// copy sine
!:			lda sine128,x
			sta sine128+$100,x
			dex
			bpl !-

//------------------------------------------------------------------------------
mainLoop:
			ldx #1
!:			lda cntStart,x
			clc
			adc sineSpeeds,x
			sta cntStart,x
			sta cnt,x
			stx ss+1
			dex
			bpl !-
			
			lda #>screen
			sta ss+2
!yLoop:
!barLoop:
			lax cnt+1
			clc
			adc sine128 + sineStarts.get(1),x
			ldx cnt+0
			clc
			adc sine128 + sineStarts.get(0),x
			tax
			
			and #$80
			sta barOrder
			
			txa
			eor barCnt
			eor barOrder
			tay
			lda sine128,y
			lsr
			lsr
			lsr
			adc #buffer+offsetX
			sta sb0+1
			adc #$f8
			tay
			
			ldx #<colors0
			lda barCnt
			eor barOrder
			bpl !+
			ldx #<colors1
!:			stx lcl+1
			
			ldx #$08
!xLoop:
lcl:			lda colors0,x
sb0:			sta buffer,x
sb1:			sta buffer,y
			iny
			dex
			bpl !xLoop-
			
			lda barCnt
			eor #$80
			sta barCnt
			bmi !barLoop-

			ldx #$27
			ldy #$ee
!:			lda buffer,x
ss:			sta screen,x
			sty buffer,x
			dex
			bpl !-
			
			ldx #1
!:			lda cnt+0,x
			clc
			adc #sineSpread
			sta cnt+0,x
			dex
			bpl !-
						
			lda #screenWidth
			clc
			adc ss+1
			sta ss+1
			bcc !+
			inc ss+2
!:			cmp #<[screen + screenWidth * screenHeight]
			bne !yLoop-
	
			jmp mainLoop

sineSpeeds:		.fill sineSpeedList.size(), sineSpeedList.get(i)
